package vzaar

import scala.collection.immutable.Map
import scala.xml._ 
import scala.xml.path._ 

/**
 * Utility object for handling things like XML, accepted formats and the
 * like
 * 
 * @author Marc G. Smith
 */
object VzaarUtil 
{
	/**
	 * Delete request XML.
	 */
	private[vzaar] val DELETE_VIDEO =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
		"<vzaar_api>\n" +
	    "   <_method>delete</_method>\n" +
	    "</vzaar_api>\n"
 
	/**
	 * Process request XML for use with formatter.
	 * Takes four parameters - the GUI, the title, the description and
	 * the profile type. 
	 */
	private[vzaar] val PROCESS_VIDEO =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
		"<vzaar_api>\n" +
	    "   <video>\n" +
	    "      <guid>%s</guid>\n" +
	    "      <title>%s</title>\n" +
	    "      <description>%s</description>\n" +
	    "      <profile>%s</profile>\n" +
	    "   </video>\n" +
	    "</vzaar_api>\n";
	
	
	/**
	 * Change video request XML.
	 * Takes two parameters -the title and the description. 
	 */
	private[vzaar] val EDIT_VIDEO =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
		"<vzaar_api>\n" +
	    "   <_method>put</_method>\n" +
	    "   <video>\n" +
        "      <title>%s</title>\n" +
        "      <description>%s</description>\n" +
        "   </video>\n" +
	    "</vzaar_api>\n";
	
	/**
	 * Trusted formats for videos. 
	 */
	val trustedFormats:Map[String,Tuple2[String,String]] = Map(
		"asf" -> ("asf", "asf format"),
		"avi" -> ("avi", "avi format"),
		"flv" -> ("flv", "flv video format"),
		"m4v" -> ("m4v", "raw MPEG4 video format"),
		"mov" -> ("mov", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"mp4" -> ("mp4", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"m4a" -> ("m4a", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"3gp" -> ("3gp", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"3g2" -> ("3g2", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"mj2" -> ("mj2", "QuickTime/MPEG4/Motion JPEG 2000 format"),
		"wmv" -> ("wmv", "Windows Media Video format"))
   
	/**
 	 * Accepted formats for videos. 
	 */
	val acceptedFormats:Map[String,Tuple2[String,String]] =	trustedFormats ++ Map(
	  	"4xm" -> ("4xm", "4X Technologies format"),
	  	"MTV" -> ("MTV", "MTV format"),
		"RoQ" -> ("RoQ", "Id RoQ format"),
		"aac" -> ("aac", "ADTS AAC"),
		"ac3" -> ("ac3", "raw ac3"),
		"aiff" -> ("aiff", "Audio IFF"),
		"alaw" -> ("alaw", "pcm A law format"),
		"amr" -> ("amr", "3gpp amr file format"),
		"apc" -> ("apc", "CRYO APC format"),
		"ape" -> ("ape", "Monkey's Audio"),
		"au" -> ("au", "SUN AU Format"),
		"avs" -> ("avs", "avs format"),
		"bethsoftvid" -> ("bethsoftvid", "Bethesda Softworks 'Daggerfall' VID format"),
		"bktr" -> ("bktr", "video grab"),
		"c93" -> ("c93", "Interplay C93"),
		"daud" -> ("daud", "D -Cinema audio format"),
		"dsicin" -> ("dsicin", "Delphine Software International CIN format"),
		"dts" -> ("dts", "raw dts"),
		"dv" -> ("dv", "DV video format"),
		"dxa" -> ("dxa", "dxa"),
		"ea" -> ("ea", "Electronic Arts Multimedia Format"),
		"ffm" -> ("ffm", "ffm format"),
		"film_cpk" -> ("film_cpk", "Sega FILM/CPK format"),
		"flac" -> ("flac", "raw flac"),
		"flic" -> ("flic", "FLI/FLC/FLX animation format"),
		"gif" -> ("gif", "GIF Animation"),
		"gxf" -> ("gxf", "GXF format"),
		"h261" -> ("h261", "raw h261"),
		"h263" -> ("h263", "raw h263"),
		"h264" -> ("h264", "raw H264 video format"),
		"idcin" -> ("idcin", "Id CIN format"),
		"image2" -> ("image2", "image2 sequence"),
		"image2pipe" -> ("image2pipe", "piped image2 sequence"),
		"ingenient" -> ("ingenient", "Ingenient MJPEG"),
		"ipmovie" -> ("ipmovie", "Interplay MVE format"),
		"matroska" -> ("matroska", "Matroska File Format"),
		"mjpeg" -> ("mjpeg", "MJPEG video"),
		"mm" -> ("mm", "American Laser Games MM format"),
		"mmf" -> ("mmf", "mmf format"),
		"mp3" -> ("mp3", "MPEG audio layer 3"),
		"mpc" -> ("mpc", "musepack"),
		"mpeg" -> ("mpeg", "MPEG1 System format"),
		"mpegts" -> ("mpegts", "MPEG2 transport stream format"),
		"mpegtsraw" -> ("mpegtsraw", "MPEG2 raw transport stream format"),
		"mpegvideo" -> ("mpegvideo", "MPEG video"),
		"mulaw" -> ("mulaw", "pcm mu law format"),
		"mxf" -> ("mxf", "MXF format"),
		"nsv" -> ("nsv", "NullSoft Video format"),
		"nut" -> ("nut", "nut format"),
		"nuv" -> ("nuv", "NuppelVideo format"),
		"ogg" -> ("ogg", "Ogg format"),
		"oss" -> ("oss", "audio grab and output"),
		"psxstr" -> ("psxstr", "Sony Playstation STR format"),
		"rawvideo" -> ("rawvideo", "raw video format"),
		"redir" -> ("redir", "Redirector format"),
		"rm" -> ("rm", "rm format"),
		"rtsp" -> ("rtsp", "RTSP input format"),
		"s16be" -> ("s16be", "pcm signed 16 bit big endian format"),
		"s16le" -> ("s16le", "pcm signed 16 bit little endian format"),
		"s8" -> ("s8", "pcm signed 8 bit format"),
		"sdp" -> ("sdp", "SDP"),
		"shn" -> ("shn", "raw shorten"),
		"smk" -> ("smk", "Smacker Video"),
		"sol" -> ("sol", "Sierra SOL Format"),
		"swf" -> ("swf", "Flash format"),
		"thp" -> ("thp", "THP"),
		"tiertexseq" -> ("tiertexseq", "Tiertex Limited SEQ format"),
		"tta" -> ("tta", "true -audio"),
		"txd" -> ("txd", "txd format"),
		"u16be" -> ("u16be", "pcm unsigned 16 bit big endian format"),
		"u16le" -> ("u16le", "pcm unsigned 16 bit little endian format"),
		"u8" -> ("u8", "pcm unsigned 8 bit format"),
		"vc1" -> ("vc1", "raw vc1"),
		"vmd" -> ("vmd", "Sierra VMD format"),
		"voc" -> ("voc", "Creative Voice File format"),
		"wav" -> ("wav", "wav format"),
		"wc3movie" -> ("wc3movie", "Wing Commander III movie format"),
		"wsaud" -> ("wsaud", "Westwood Studios audio format"),
		"wsvqa" -> ("wsvqa", "Westwood Studios VQA format"),
		"wv" -> ("wv", "WavPack"),
		"yuv4mpegpipe" -> ("yuv4mpegpipe", "YUV4MPEG pipe format"))

	/**
	 * Provided with a file name or just the extension this method will 
	 * return true if the file format is a trusted format to be
	 * decoded by <a href="http://vzaar.com">vzaar.com</a>. 
	 * 
	 * @param fileNameOrExtension the full filename or just the extension
	 * @return true if a trusted and tested file format, false otherwise
	 */
	def isTrustedFormat(fileNameOrExtension:String):Boolean = {
		trustedFormats(normaliseFileExtension(fileNameOrExtension)) != null
	}

	/**
	 * Provided with a file name or just the extension this method will 
	 * return true if the file format is an accepted format. This includes
	 * trusted as well as untrusted formats. Untrusted formats are not
	 * well tested and are at your own risk. 
	 * 
	 * @param fileNameOrExtension the full filename or just the extension
	 * @return true if a accepted file format, false otherwise
	 */
	def isAcceptedFormat(fileNameOrExtension:String):Boolean = {
		acceptedFormats(normaliseFileExtension(fileNameOrExtension)) != null
	}
	
	/**
	 * Normalise a filename or extension to enable lookup of extension maps.
	 * 
	 * @param fileNameOrExtension the full filename or just the extension
	 * @return the extension
	 */
	private def normaliseFileExtension(fileNameOrExtension:String):String = {
		var index = fileNameOrExtension.lastIndexOf(".")
		var normalisedName = fileNameOrExtension
		if(index > -1) {
			normalisedName = fileNameOrExtension.substring(index + 1)
		}
		
		normalisedName.toLowerCase;
	}

	protected[vzaar] def xmlAsString(document:Node, tag:String):String = {
		//return document \\ tag + " | */" + tag + "| */*/" + tag
		var e:NodeSeq = document \\ tag
		if(e.size == 0) return null
		return e.first.text
    }
 
	protected[vzaar] def xmlNodes(document:Node, tag:String):NodeSeq = {
		return document \\ tag
	}
}


